package com.app.backend.common.utils;

import java.util.Random;

/**
 * @program: RandomUtil
 * @description: 随机数相关工具
 * @author: Zhun.Xiao
 * @create: 2019-06-25 07:59
 **/
public class RandomUtil {

    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }
    
    public static String getHexRandomString(int length) {
        String str = "abcdefABCDEF0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(22);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }


    public static void main(String[] args) {
        System.out.println(getRandomString(16));
        System.out.println(getHexRandomString(16));
    }
}